import smtplib
from email.mime.text import MIMEText
from email.header import Header

HOST = "smtp.sohu.com"
SENDER = "linlinyue520@sohu.com"                # 发送方邮箱
RECEIVERS = ["xxx@163.com"]                     #　接收方邮箱
USER = "linlinyue520"
PSW = "***"


class  EmailSendHandler(object):
    def __init__(self, sender, receivers, host, user, psw):
        self.sender = sender
        self.receivers = receivers
        self.host = host
        self.user = user
        self.psw = psw


    def send_email(self, title, content):
        mimetext = MIMEText(content, 'plain', 'utf-8')
        mimetext["Subject"] = Header(title, "utf-8")       # 邮件标题
        mimetext["From"] = Header("尤里")
        recervers = ",".join(self.receivers)
        mimetext["To"] = Header(recervers)
        print(mimetext.as_string())

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(self.host, 25)
            smtpObj.login(self.user, self.psw)
            smtpObj.sendmail(self.sender, self.receivers, mimetext.as_string())
            smtpObj.quit()
            print("邮件发送成功")
        except smtplib.SMTPException as e:
            print(e)
            print("发送失败！")


    def set_title(self, title=None):
        pass


if __name__ == "__main__" :
    emailObj = EmailSendHandler(SENDER, RECEIVERS, HOST, USER, PSW)
    content = """
    无名字的你 如永远的传奇
    但怎不经意的 偷偷进入心扉
    """
    emailObj.send_email("python测试", content)


